package com.cxs.xiaomi.web.controller;

import com.cxs.xiaomi.domain.Goods;
import com.cxs.xiaomi.domain.User;
import com.cxs.xiaomi.service.GoodsService;
import com.cxs.xiaomi.service.impl.GoodsServiceImpl;
import com.cxs.xiaomi.utils.DatabackUtil;
import com.cxs.xiaomi.utils.Page;
import com.cxs.xiaomi.utils.StringUtil;
import cxs.utils.utils.Result;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/*
 * @Author:cxs
 * @Date:2021/9/14  16:13
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@WebServlet(name = "GoodsServlet", urlPatterns = "/goodsservlet")
public class GoodsServlet extends BaseServlet {

    private GoodsService goodsService = new GoodsServiceImpl();
    // 根据类型id获得商品
    public String getGoodsListByTypeId(HttpServletRequest request, HttpServletResponse response){
        String typeId = request.getParameter("typeId");
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        // 封装动态SQL
        StringBuilder sb = new StringBuilder();
        List<Object> params = new ArrayList<>();
        sb.append(" where 1=1 ");
        if(!StringUtil.empty(typeId)){
            sb.append("and typeid = ? ");
            params.add(typeId);
        }
        Page<Goods> page = goodsService.findGoodByType(pageNum, pageSize, sb.toString(), params);
        request.setAttribute("pageBean",page);
        request.setAttribute("typeid",typeId);
        return "forward:/goodsList.jsp";
    }

    // 根据id获得商品详情
    public String getGoodsById(HttpServletRequest request, HttpServletResponse response){
        String id = request.getParameter("id");
        if(StringUtil.empty(id)){
            request.setAttribute("msg","id必传");
            return "forward:/message.jsp";
        }
        Goods good = goodsService.getDetail(id.trim());
        request.setAttribute("goods",good);
        return "forward:/goodsDetail.jsp";
    }

    public String getGoodsListByName(HttpServletRequest request, HttpServletResponse response){
        String typeId = request.getParameter("typeId");
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        String name = request.getParameter("name");
        request.setAttribute("name",name);
        // 封装动态SQL
        StringBuilder sb = new StringBuilder();
        List<Object> params = new ArrayList<>();
        sb.append(" where 1=1 ");
        if(!StringUtil.empty(name)){
            sb.append("and name like ? ");
            params.add("%" + name.trim() + "%");
        }
        Page<Goods> page = goodsService.findGoodByType(pageNum, pageSize, sb.toString(), params);
        request.setAttribute("pageBean",page);
        request.setAttribute("typeid",typeId);
        return "forward:/goodsList.jsp";
    }

    // 管理员
    // 查询所有商品信息
    public String getGoods(HttpServletRequest request, HttpServletResponse response){
        User admin = getAdmin(request, response);
        if(admin != null){
            String name = request.getParameter("name");
            String pubdate = request.getParameter("pubdate");
            String pageNum = request.getParameter("pageNum");
            String pageSize = request.getParameter("pageSize");
            DatabackUtil.dataReback(request);
            StringBuilder sb = new StringBuilder();
            sb.append(" where 1=1 ");
            List params = new ArrayList();
            if(!StringUtil.empty(name)){
                sb.append("and name like ? ");
                params.add("%" + name.trim() + "%");
            }
            if(!StringUtil.empty(pubdate)){
                sb.append("and pubdate = ? ");
                params.add(pubdate.trim());
            }
            if(StringUtil.empty(pageNum)){
                pageNum = "1";
            }
            if(StringUtil.empty(pageSize)){
                pageSize = "5";
            }
            try {
                Page<Goods> page = goodsService.getGoodsByPageBean(pageNum.trim(),pageSize.trim(),sb.toString(),params);
                request.setAttribute("pageBean",page);
                return "forward:/admin/showGoods.jsp";
            } catch (NumberFormatException e) {
                e.printStackTrace();
                request.setAttribute("msg","添加失败");
                return "forward:/message.jsp";
            }
        }
        return "redirect:/admin/login.jsp";
    }

    // 根据id删除
    public Object delGoods(HttpServletRequest request, HttpServletResponse response){
        String id = request.getParameter("id");
        if(StringUtil.empty(id)){
            return Result.failare("id不能为空");
        }
        try {
            return goodsService.delete(Integer.parseInt(id)) ? Result.OK("删除成功") : Result.failare("删除失败");
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return Result.failare("删除失败");
    }


    // 测试
    public void test(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("测试方法执行");
    }
}
